package cn.janyd.action;

import cn.janyd.action.template.*;
import cn.janyd.action.utils.PsiFileUtils;
import com.intellij.database.psi.DbTable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;

/**
 * @author Janyd
 * @since 2020-08-09 3:51
 */
public class GeneratorService {

    private final TemplateEngine templateEngine = new FreemarkerTemplateEngine();

    /**
     * 生成逻辑
     *
     * @param configBuilder 配置信息
     * @param project       项目
     * @param psiElements   选中的表信息
     */
    public void generate(ConfigBuilder configBuilder, Project project, PsiElement[] psiElements) {

        // async write action
        ApplicationManager.getApplication().runWriteAction(() -> {

            WriteAction.run(() -> {
                String date = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
                String author = System.getProperty("user.name");

                Arrays.stream(psiElements).filter(p -> p instanceof DbTable).forEach(p -> {
                    TableInfo tableInfo = new TableInfo((DbTable) p);
                    DataObjectClass dataObjectClass = configBuilder.buildDataObjectClass(tableInfo, date, author);
                    MapperClass mapperClass = configBuilder.buildMapperClass(dataObjectClass, date, author);
                    MapperXml mapperXml = configBuilder.buildMapperXml(dataObjectClass, mapperClass);
                    try {
                        VirtualFile dataObjectPackage = PsiFileUtils.findPackageDir(configBuilder.getDataObjectPackage(), configBuilder.getDataObjectModulePath());
                        VirtualFile dataObjectFile = dataObjectPackage.findOrCreateChildData(project, dataObjectClass.getClassName() + Constants.DOT_JAVA);
                        dataObjectFile.setBinaryContent(templateEngine.generate(dataObjectClass));

                        if (configBuilder.isEnableMapper()) {
                            VirtualFile mapperPackage = PsiFileUtils.findPackageDir(configBuilder.getMapperPackage(), configBuilder.getMapperModulePath());
                            VirtualFile mapperClassFile = mapperPackage.findOrCreateChildData(project, mapperClass.getMapperName() + Constants.DOT_JAVA);
                            mapperClassFile.setBinaryContent(templateEngine.generate(mapperClass));
                        }

                        if (configBuilder.isEnableMapperXml()) {
                            VirtualFile mapperXmlPath = PsiFileUtils.findPath(configBuilder.getMapperXmlPath());
                            VirtualFile mapperXmlFile = mapperXmlPath.findOrCreateChildData(project, mapperXml.getMapperName() + Constants.DOT_XML);
                            mapperXmlFile.setBinaryContent(templateEngine.generate(mapperXml));
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                });
            });
        });
    }
}
